import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from '../../../core/api/api.service';
import { MessageService } from '../../../core/service/message.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
@Component({
    selector: 'app-role-mgmt',
    templateUrl: './role-mgmt.component.html',
})
export class RoleMgmtComponent implements OnInit {
    roleList = []; // 列表数据
    page: any = {pageNo: 1, pageSize: 10};
    isVisible = false; // 是否显示弹出框
    modalTitle; // 弹出框标题
    isOkLoading = false; // 弹出框按钮加载动画
    tableLoading = false;
    validateForm;
    activeData: any = {};
    ngOnInit() {
        this.message.setBreadcrumb({
            breadcrumbs: [
            {name: '系统角色管理'}
        ]});
        this.validateForm = this.fb.group({
            name: [null, [ Validators.required]],
            memo: [null],
            status: [null],
        });
        this.getRoleList();
    }
    getRoleList(params = {}) {
        this.tableLoading = true;
        this.apiService.post('角色列表', {...params, ...this.page}).then((res: any) => {
            this.roleList = res.data.list;
            this.tableLoading = false;
        }).catch(res => {
            this.tableLoading = false;
        });
    }
    handleOk() {
        this.isOkLoading = true;
        this.apiService.post('创建角色', {...this.validateForm.getRawValue(), status: 'Y'}).then((res: any) => {
            this.isOkLoading = false;
            this.isVisible = false;
            this.nzMessageService.success('创建成功！');
            this.getRoleList();
        }).catch(res => {
            this.isOkLoading = false;
        });
    }
    handleCancel() {
        this.isVisible = false;
    }
    addRole() {
        this.isVisible = true;
        this.modalTitle = '新增角色';
        this.validateForm.reset();
    }
    editRole(item) {
        sessionStorage.setItem('roleData', JSON.stringify(item));
        this.router.navigate(['systemMgmt/roleAuthority']);
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService
    ) { }
}
